﻿using System;
using System.ComponentModel;
using System.Reflection;
using System.Resources;
using System.Threading;
using System.Web.UI.WebControls.WebParts;

namespace SharePoint.Sunbox.Sandbox.Core
{
    #region Nested type: LocalResourceManager

    internal class LocalResourceManager
    {
        public static LocalResourceManager Instance
        {
            get { return new LocalResourceManager(); }
        }

        public string GetString(string resourceName)
        {
            // read the title from the Assembly Properties = Default Namespace
            object[] attributes = GetType().Assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
            var ata = (AssemblyTitleAttribute)attributes[0];

            // get the fully qualified path to the resource
            string resourceType = String.Format("{0}.Resources.Properties", ata.Title);

            // read the localized string
            var rm = new ResourceManager(resourceType, GetType().Assembly);
            return rm.GetString(resourceName, Thread.CurrentThread.CurrentUICulture);
        }
    }

    #endregion

    #region Nested type: LocalizedCategoryAttribute

    public sealed class LocalizedCategoryAttribute : CategoryAttribute
    {
        public LocalizedCategoryAttribute(string category)
            : base(category)
        {
        }

        protected override string GetLocalizedString(string value)
        {
            return LocalResourceManager.Instance.GetString(value);
        }
    }

    #endregion

    #region Nested type: LocalizedWebDescriptionAttribute

    public sealed class LocalizedWebDescriptionAttribute : WebDescriptionAttribute
    {
        private bool _isLocalized;

        public LocalizedWebDescriptionAttribute(string description)
            : base(description)
        {
        }

        public override string Description
        {
            get
            {
                if (!_isLocalized)
                {
                    DescriptionValue = LocalResourceManager.Instance.GetString(base.Description);
                    _isLocalized = true;
                }
                return base.Description;
            }
        }
    }

    #endregion

    #region Nested type: LocalizedWebDisplayNameAttribute

    public sealed class LocalizedWebDisplayNameAttribute : WebDisplayNameAttribute
    {
        private bool _isLocalized;

        public LocalizedWebDisplayNameAttribute(string displayName)
            : base(displayName)
        {
        }

        public override string DisplayName
        {
            get
            {
                if (!_isLocalized)
                {
                    DisplayNameValue = LocalResourceManager.Instance.GetString(base.DisplayName);
                    _isLocalized = true;
                }
                return base.DisplayName;
            }
        }
    }

    #endregion
}
